package zy.wq.config;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import zy.wq.common.InvocationResult;


public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {

	private HttpOutputMessageWriter outputMessageWriter;
	
	
	@Override
	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException authException)
			throws IOException, ServletException {
		response.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
		Object errorResult = createErrorResult(request,response,authException);
		outputMessageWriter.write(request, response, errorResult);
	}
	
	protected Object createErrorResult(HttpServletRequest request,
			HttpServletResponse response,AuthenticationException authException) {
		return new InvocationResult(InvocationResult.FAILED,"403",null);
	}

	public HttpOutputMessageWriter getOutputMessageWriter() {
		return outputMessageWriter;
	}

	public void setOutputMessageWriter(HttpOutputMessageWriter outputMessageWriter) {
		this.outputMessageWriter = outputMessageWriter;
	}

	
	
	

}
